<?php

namespace Home\Model;

use Think\Model;

class SaleChanceModel extends Model {

    protected $insertFields = array('chance_id', 'chance_from', 'client_name', 'ok_probable', 'outline', 'contact_name', 'contact_phone', 'description', 'create_name', 'create_time');
    protected $updateFields = array('id', 'chance_id', 'chance_from', 'client_name', 'ok_probable', 'outline', 'contact_name', 'contact_phone', 'description', 'give_name', 'give_time', 'status');
    protected $_validate = array(
        array('chance_id', '0,20', '编号的值不能超过 20 个字符！', 1, 'length', 3),
        array('chance_id', 'chck_id', '编号已存在！', 1, 'callback', 1),
        array('chance_from', '0,20', '机会来源的值最长不能超过 20 个字符！', 1, 'length', 3),
        array('client_name', 'require', '客户名称不能为空！', 1, 'regex', 3),
        array('client_name', '1,20', '客户名称的值最长不能超过 20 个字符！', 1, 'length', 3),
        array('ok_probable', 'number', '成功几率必须是1到100之间的整数！', 1, 'regex', 3),
        array('ok_probable', '1,100', '成功几率必须是1到100之间的整数！', 1, 'between', 3),
        array('outline', 'require', '概要不能为空！', 1, 'regex', 3),
        array('outline', '1,50', '概要的值最长不能超过 20 个字符！', 1, 'length', 3),
        array('contact_name', '0,20', '联系人的值最长不能超过 20 个字符！', 1, 'length', 3),
        array('contact_phone', '0,12', '联系人电话的值不能超过 12 个字符！', 1, 'length', 3),
        array('description', 'require', '描述不能为空！', 1, 'regex', 3),
        array('description', '1,200', '描述的值最长不能超过 20 个字符！', 1, 'length', 3),
    );
    public $rules = array(
        array('give_name', 'require', '指派给谁不能为空！', 1, 'regex', 7),
        array('give_time', 'require', '指派时间不能为空！', 1, 'regex', 7),
    );

    public function chck_id($id) {
        if ($id != null) {
            $va = $this->where(array('chance_id' => array('eq', $id)))->find();
            if ($va) {
                $this->error = '编号不唯一!';
                return FALSE;
            }
        }
        return TRUE;
    }

    public function search($which = 0, $pageSize = 20) {
        /************************************************** 搜索 *************************************** */
        /***********************客户经理限制权限 ************************* */
        $where = array();
        
        $sid = session('id');
        $sname = session('username');
        $adModel = M('Admin_role');
        $rid = $adModel->field('role_id')->where(array('admin_id' => array('eq', $sid)))->find();
        if ($rid['role_id'] == '4') {
            $where['give_name'] = array('eq', "$sname");
        }
        
        if ($which == '0')
            $where['status'] = array('eq', '0');
        else
            $where['status'] = array('neq', '0');
        if ($client_name = I('get.client_name'))
            $where['client_name'] = array('like', "%$client_name%");
        if ($outline = I('get.outline'))
            $where['outline'] = array('like', "%$outline%");
        if ($contact_name = I('get.contact_name'))
            $where['contact_name'] = array('like', "%$contact_name%");
        /*         * *********************************** 翻页 *************************************** */
        $count = $this->alias('a')->where($where)->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $data['page'] = $page->show();
        /*         * ************************************ 取数据 ***************************************** */
        $data['data'] = $this->where($where)->order('give_time desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        return $data;
    }

    protected function _before_insert(&$data, $options) {
        //$time = I('post.create_time');
        $data['create_time'] = time();
    }

    protected function _before_update(&$data, $options) {
        $time = I('post.give_time');
        $da11 = $data['give_time'] = strtotime($time);
        if ($time) {
            $data['status'] = 1;
        }
    }

}
